var address = require('../../utils/cityData.js')
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var config = require('../../config')
var util = require('../../utils/util.js')
//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList: [],
    userid:app.globalData.userInfo.userid,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    //var userInfoStorage = wx.getStorageSync('userInfo');
    wx.getStorage({
      key:'userInfo',
      success:function(res){
        var userInfoStorage = res.data;
        if (!util.isNUll(userInfoStorage)&&!util.isNUll(userInfoStorage.userid)) {
          app.globalData.userInfo = userInfoStorage;
          that.setData({
            userid:userInfoStorage.userid,
            hasUserInfo: true,
          })
          that.initShippingAddress()
        }else{
          that.login();
        }
      }});
  },
  login: function (callback) {
    var that = this;
    app.login(function(e){
          wx.getStorage({
            key:'userInfo',
            success:function(res){
              let userInfo = res.data;
              app.globalData.userInfo = userInfo;
              that.setData({
                userid:userInfo.userid,
                hasUserInfo: true,
              })
              that.initShippingAddress()
            }
          })})
},
  initShippingAddress: function () {
    wx.request({
      url: config.service.addressList,
      data: {
        userid: this.data.userid,
      },
      success: (res) => {
      this.setData({
          addressList: res.data.data
        })
      },
    })
  },

  selectTap: function (e) {
    var idx = e.currentTarget.dataset.id
    var addr = this.data.addressList[idx]
    var userid = this.data.userid
    var postData = {
      userid: userid,
      id: addr.id,
      name: addr.name,
      tel: addr.tel,
      province: addr.province,
      city: addr.city,
      area: addr.area,
      address: addr.address,
      status: 1
    }
    if (addr.status != 1) {
      wx.request({
        url: config.service.addressUpdate,
        data: postData,
              method: 'POST',
              header: {
                  'content-type':'application/x-www-form-urlencoded'
              },
        success: (res) => {
          if (res.data.code == 0) {
            this.initShippingAddress()
          }
        }
      })
    }
  },

  editAddess: function (e) {
    wx.navigateTo({
      url: "/pages/add-addr/index?id=" + e.currentTarget.dataset.id + '&editType=upData'
    })
  },

  onAddAddrTap: function () {
    wx.navigateTo({
      url: '/pages/add-addr/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initShippingAddress()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})